package demo1;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-04
 * Time: 19:42
 */
public class MyLinkedList {
     static class Node{
         public int val;
         public Node next;

         public Node(int val){
             this.val = val;
         }
     }

     Node head = null;//头结点

    //头插法
    public void addFirst(int data){
         Node node = new Node(data);//data这个新节点
         if(head==null){
             head = node;
         }else {
             node.next = head;
             head = node;
         }
         size();
    }
    //尾插法
    public void addLast(int data){
         Node node = new Node(data);
         if(head==null){
             head = node;
         }else {
             //找尾
             Node cur = head;
             while(cur.next!=null){
                 cur = cur.next;
             }
             cur.next = node;
         }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        //检查index下标合不合法
        if(index<0||index>size()){
            System.out.println("该下标不合法...");
            return false;
        }
        if(index==0){
            addFirst(data);
            return true;
        }
        if(index==(size()-1)){
            addLast(data);
            return true;
        }

        Node node = new Node(data);
        //走index-1步找到它的前一个
        Node cur = head;
        while((index-1)!=0){
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur = cur.next;
        }
          return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head==null){
            System.out.println("该链表为空，不能删除！！！");
            return;
        }
        //处理删除头结点的情况
        if(head.val==key){
            head = head.next;
            return;
        }
        //处理正常情况
        Node cur = head;
        while(cur.next!=null){
                Node del = cur.next;
                if(cur.next.val==key){
                    //找到要删除的节点
                    cur.next = del.next;
                    return ;
                }
                cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null) return;

        //处理从头到尾都是key
        while(head!=null&&head.val==key){
            head = head.next;
        }
        //此时头结点不为key
        Node prev = null;
        Node cur = head;
        while(cur!=null){
            if(cur.val==key){
                prev.next = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }

    }
    //得到单链表的长度
    public int size(){
        int sz =0;
        Node cur = head;
        while(cur!=null){
            sz++;
            cur = cur.next;
        }
        return sz;
    }

    public void display(){
         Node cur = head;
         while(cur!=null){
             System.out.print(cur.val+" ");
             cur = cur.next;
         }
    }
    public void clear(){

    }

}
